package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.feign.BehaviorFeign;
import com.usian.article.mapper.ApArticleConfigMapper;
import com.usian.article.mapper.ApArticleMapper;
import com.usian.article.service.ApArticleConfigService;
import com.usian.article.service.ApArticleContentService;
import com.usian.article.service.ApArticleService;
import com.usian.model.article.dto.ArticleDto;
import com.usian.model.article.dto.ArticleHomeDto;
import com.usian.model.article.dto.ArticleInfoDto;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApFollowBehavior;
import com.usian.model.behavior.pojos.ApLikesBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;
    @Value("${fileServerUrl}")
    private String fileServerUrl;
    @Autowired
    private ApArticleConfigService apArticleConfigService;
    @Autowired
    private ApArticleContentService apArticleContentService;

    @Autowired
    private BehaviorFeign behaviorFeign;


    @Override
    public ApArticle saveApArticle(ApArticle apArticle) {
        apArticle.setCreatedTime(new Date());
        this.save(apArticle);
        return apArticle;
    }

    @Override
    public ResponseResult loadList(Short type, ArticleHomeDto dto) {

        //校验参数
        if(dto==null || dto.getSize()==null ||dto.getMinBehotTime()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if( StringUtils.isBlank(dto.getTag())){
            dto.setTag("__all__");
        }

        dto.setMinBehotTime(new Date());
        List<ApArticle> apArticleList = apArticleMapper.loadArticleList(dto, type);


        ResponseResult responseResult = ResponseResult.okResult(apArticleList);
        responseResult.setHost(fileServerUrl);
        return responseResult;
    }

    @Override
    public ResponseResult loadDetail(ArticleDto dto) {
        if(dto==null ||dto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //config  content
        Map<String,Object> result = new HashMap<>();
        ApArticleConfig apArticleConfig = apArticleConfigService.getOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));
        if(apArticleConfig!=null){

            //没下线  没删除
            if(!apArticleConfig.getIsDown()&&!apArticleConfig.getIsDelete()){
                result.put("config",apArticleConfig);

                //查文章内容
                ApArticleContent apArticleContent = apArticleContentService.getOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));
                result.put("content",apArticleContent);
            }
        }
        return ResponseResult.okResult(result);
    }
    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        Map<String, Object>  map = new HashMap<>();
        if (dto==null || dto.getArticleId()==null || dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(apUser.getId(), dto.getEquipmentId());
        if (apBehaviorEntry!=null){
            List<ApFollowBehavior> followBehavior = behaviorFeign.findFollowBehavior(apBehaviorEntry.getId(), dto.getArticleId(), dto.getAuthorId());
            if (followBehavior.size()>0){
                map.put("isFollow",true);
            }else {
                map.put("isFollow",false);
            }
            ApLikesBehavior apLikesBehavior = behaviorFeign.findLike(apBehaviorEntry.getId(), dto.getArticleId());
            if (apLikesBehavior!=null){
                map.put("isLike",true);
            }else {
                map.put("isLike",false);
            }
        }
        return ResponseResult.okResult(map);
    }

}
